<?
class iMVC_View_Table {
    
    protected $table;
    protected $tbody;
    protected $thead;
    protected $tfoot;
    protected $doc;
    protected $dataSource=array();
    protected $costumRows=array();
    protected $costumCols=array();
    protected $colArray=array();
    protected $rowArray=array(); 
    protected $defaultColGenerator=iMVC_View_Table_Col_Default;
    protected $defaultRowGenerator=iMVC_View_Table_Row_Default;
    public $colOrder=array();
    public $colGenerator=array();
    public $colName=array();
    public $hideColArray=array();
    public $className='iweb-table';
    public $showCss=true;

    function __construct(){
            

        $this->doc = new DOMDocument('1.0');
        $this->doc->formatOutput = true;

        $this->table=$this->doc->createElement('table');
        $this->tbody=new DOMElement('tbody');
        $this->thead=new DOMElement('thead');
        $this->tfoot=new DOMElement('tfoot');

        $this->table->appendChild($this->thead); 
        $this->table->appendChild($this->tbody); 
        $this->table->appendChild($this->tfoot); 

    }
    function generateTable(array $_dataSource){
        //init Table
        $this->table->setAttribute('class',$this->className);


        //<<<
        $this->dataSource=$_dataSource;
        $tmpArray=array();

        foreach($this->dataSource[0] as $key=>$value ){
            //[0=>iMVC_View_Table_Col_Node_Abstract,1=>sortNum]
            
            if($this->colGenerator[$key]!=null){
                $tmpArray[$key][0]=new $this->colGenerator[$key];
            }
            else{
                $tmpArray[$key][0]=new $this->defaultColGenerator;
            }
             

            $tmpArray[$key][1]=$this->colOrder[$key]==null?0:$this->colOrder[$key];
        }
        //bug:array_merge导致列名为数字时排序错误
        $this->colArray=array_merge($tmpArray,$this->colArray);
         


        //echo "<br>";
        //echo "****************************************************************";
        //echo "<br>";
        //var_dump($this->colArray);
        //echo "<br>";
        //echo "****************************************************************";
        //echo "<br>";
        //对列进行排序<<<
        
        $sortArray=array();
        $tempArray=$this->colArray;

        while(list($key,$value)=each($tempArray)){
            //echo "----------array-------------<br>";
            //echo "<br>";
            //var_dump($tempArray);
            //echo "key===>".$key."<====<br>";
            $tmpKey=$tmpKey==null?$key:$tmpKey;
            $tmpValue=$tmpValue==null?$value:$tmpValue;
            
                foreach($tempArray as $akey=>$avalue){

                    if($key!=$akey){

                        if($tmpValue[1]>$avalue[1]){ 

                            //echo "key:".$key.':'.$tmpValue[1].' > '.$akey.':'.$avalue[1]."<br/>";
                            $tmpValue=$tmpValue;
                            $tmpKey=$tmpKey;
                            //echo '---------->key:'.$tmpKey.',value:'.$tmpValue[1].'<br/>';
                        }
                        else{ 
                            //echo "key:".$key.':'.$tmpValue[1].' <= '.$akey.':'.$avalue[1]."<br/>";
                            $tmpValue=$avalue;
                            $tmpKey=$akey;
                            //echo '!!!------->key:'.$tmpKey.',value:'.$tmpValue[1].'<br/>';
                        }
                    }
                }

            //echo '--->key:'.$tmpKey.',value:'.$tmpValue[1].'<br/>';
            $sortArray[$tmpKey]=$tmpValue;
            unset($tempArray[$tmpKey]);
            unset($tmpValue);
            unset($tmpKey);
            //echo "----------array-------------<br>";
            //echo "<br>";
            //var_dump($tempArray);
            reset($tempArray);

            

        }


        $this->colArray=$sortArray;
        //echo "<br>$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$<br>";
        //var_dump($sortArray);
        //>>>
        
        //列名
        foreach($this->colArray as $key=>$value){
            if(!$this->hideColArray[$key]){
                $c=new $this->defaultColGenerator();
                $c->tagName='th';
                $cNode=$c->getNode(null,null,$this->dataSource,$this->doc);
                $name=$this->colName[$key]==null?$key:$this->colName[$key];
                $cNode->nodeValue=$name;
                $cNode->setAttribute('class',$key);
                $this->thead->appendChild($cNode);
            }
        } 
        //记录
        foreach($this->dataSource as $rowKey=>$rowValue){


            $r=new $this->defaultRowGenerator();
            $rNode=$r->getNode($rowKey,null,$this->dataSource,$this->doc);
            $this->tbody->appendChild($rNode); 
            foreach($this->colArray as $colKey=>$colValue){
                if(!$this->hideColArray[$colKey]){
                    $g=$colValue[0];
                    $gNode=$g->getNode($rowKey,$colKey,$this->dataSource,$this->doc);
                    $rNode->appendChild($gNode);
                }
            }
        } 
        //>>>
    }

    public function addCol($key,$gNode,$sortNum){

        $this->colArray[$key][0]=$gNode; 
        $this->colArray[$key][1]=$sortNum;
        //echo "<br>".$key.":".$this->colArray[$key][1].'<br>';
    }
    //设置列的格式 $key 列名，$gNode生成器，$sortNum排序号
    public function setCol($key,$gNode,$sortNum=null){

        $this->colArray[$key][0]=$gNode; 
        $this->colArray[$key][1]=$sortNum;
    }
    public function setColOrder($key,$order){

        $this->colOrder[$key]=$order; 

    }
    public function setColName($key,$value){

        $this->colName[$key]=$value; 

    }
    public function hideCol($key){
    
        $this->hideColArray[$key]=true; 
    
    }
    public function setColGenerator($key,$value){
        $this->colGenerator[$key]=$value; 

    }
    public function setClassName($value){
        $this->className=$value; 
    }
    public function showCss(){
    
        $str ='<style>'; 
        $str.='    .iweb-table{width:100%;border-collapse:collapse;background:#ffffff;border:#9E9E9E 1px solid;border-spacing:1px;} ';
        $str.='    .iweb-table th{background:#9E9E9E;margin:0px;border-right:#ffffff 1px solid;color:white;}';
        $str.='    .iweb-table td{border:#9E9E9E 1px solid;background:#efefef;margin:0px;text-align:center}';
        $str.='    table.iweb-table tr:hover td{background:#cccccc;cursor:pointer;color:white;};';
        $str.='</style>'; 
        return $str; 
    
    }

    function __toString(){

        if($this->showCss==true){
            $str=$this->showCss().$this->doc->saveXML($this->table); 
        }
        else{
            $str=$this->doc->saveXML($this->table); 
        }

        return $str ; 
    }
}

// vim600:ts=4 st=4 foldmethod=marker foldmarker=<<<,>>>
// vim600:syn=php commentstring=//%s
